package com.example.helloworld.controller;

import com.example.helloworld.service.TestService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
@RequestMapping("/api/test-rest")
public class TestRedisController {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("getValue")
    private String getValue(@RequestParam String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 查询所有 key（对应 Redis 命令：keys *）
     */
    @GetMapping("/getKeys")
    public Set<String> getAllKeys() {
        // 参数为 pattern，* 表示匹配所有 key
        return stringRedisTemplate.keys("*");
    }



    // 读取数据
    public String getKey(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

}
